<%@ page language="java" session="true" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:include page="../common/framework/header.jsp" />
<link href="${pageContext.request.contextPath}/static/css/testCase/corecss.css" rel="stylesheet">
<style>
	body{
		font-size:12px;color:#333;font-family:微软雅黑;background-color: #fff;
	}
  .form-inline .form-control {
    width: 100%;
	}
	.checkbox input[type=checkbox], .checkbox-inline input[type=checkbox], .radio input[type=radio], .radio-inline input[type=radio] {
    margin-top: 2px;
}
.checkbox, .radio {
    margin-top: 5px;
}
a {
    color: #000;
}
A:hover {
    text-decoration: none;
}
</style>

<div class="container-fluid" style="padding:0 40px;">
    <form id ="openAndEditTestPlanForm">
		<div class="form-inline row" style="margin-top : 15px;">
	    	<div class="form-group col-sm-12" style="margin-bottom:0px!important;">

	         		<a type="button" title="创建此工作项的副本(包括链接)" style="margin-right : 15px;" class="add_case" id="copyCase" onclick="cloneTestPlan();">
	         				<span class="icon icon-tfs-work-item-copy" style="margin-bottom:3px;"></span>
	         				 克隆
	         		</a>

	         	<hr style="margin-top: 10px;margin-bottom: 10px;">
	         </div>
	     </div>
	     <div class="form-inline row">
	         <div class="form-group col-sm-12" >
				<input type="text" class="  form-control" id="testPlanName" name="testPlanName"  placeholder="请输入测试计划名称(必填项)">
	         </div>
	     </div>
    <div class = "row" >
         	<div class="form-group col-sm-6 col-xs-6" >
            	<label class="col-xs-2" style="padding:6px 0 0 0;text-align:right;">指派给:</label>
            	<div class="col-xs-10" style="padding-right:0;">
					<select class="form-control" data-width="100%" id="assignId" name="assignId">
                     </select>
				</div>
         	</div>
         	<div class = "form-group col-sm-6 col-xs-6" >
            	<label class="col-xs-2" style="padding:6px 0 0 0;text-align:right;"><font color=red>*</font>状态:</label>
            	<div class="col-xs-10" style="padding-right:0;">
					<select class="form-control" data-width="100%" id="state" name="state">
						 <option value="0">不活动</option>
						 <option value="1">活动</option>
                     </select>
				</div>
			</div>
		</div>
    	<div class = "row" >
	    	<div class="form-group col-sm-12 col-xs-12" style="padding-left:13px;">
            <label class="col-xs-1" style="padding:6px 0 0 0;text-align:right;">时间:</label>
            <div class="col-xs-11" style="padding-right:0;">
           		<div class="col-xs-6" style="padding-left:0;padding-right:10px;">
           			 <input type="text"  placeholder=" 开始时间" id="beginTime"  name="beginTime" class=" laydate-icon"
                 		 onclick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})" style="height:32px;width:100%">
           		</div>

               	<div class="col-xs-6" style="padding:0;">
               		<input type="text"  placeholder=" 结束时间" id="endTime"  name="endTime" class=" laydate-icon"
                 		 onclick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})" style="height:32px;width:100%">
               	</div>

             </div>
         </div>
		</div>

		<div class = "row" >
	    	<div class = "form-group col-sm-12 col-xs-12" style="padding-left:13px;">
	            <label class="col-xs-1" style="padding:6px 0 0 0;text-align:right;">摘要:</label>
	            <div class="col-xs-11" style="padding-right:0;">
					<textarea class="form-control" style = " resize: none;overflow:hidden" rows="4"
            		id = "summary" name = "summary"></textarea>
				</div>
			</div>
		</div>




		<input type ="hidden"   id="testplanId" name="testplanId">
    </form>
</div>
<div id ="cloneTestPlanHtml" style="display:none;">
		<form id ="cloneTestPlanForm">
			<div style="padding: 20px 25px;"><span>创建测试计划的副本：</span></div>
			<div class="row" style="margin:0;padding-right:10px;">
		         <div class = "form-group col-sm-12 col-xs-12">
		            <label class="col-xs-1" style="padding:6px 0 0 0;text-align:right;"><font color=red>*</font>项目:</label>
		            <div class="col-xs-11" style="padding-right:0;">
		            	<select class="form-control" data-width="100%" id="projectId" name="projectId">
                     	</select>
		         	</div>
		         </div>
	         </div>
	         <div class="row" style="margin:0;padding-right:10px;">
		         <div class = "form-group col-sm-12 col-xs-12">
		            <label class="col-xs-1" style="padding:6px 0 0 0;text-align:right;"><font color=red>*</font>名称:</label>
		            <div class="col-xs-11" style="padding-right:0;">
		            	<input type="text" class=" form-control" id="planName" name="planName" placeholder = "请输入初始化被克隆计划的名称" >
		         	</div>
		         </div>
	         </div>

	    </form>
	</div>
	<jsp:include page="../common/framework/script.jsp" />
	<script src="${pageContext.request.contextPath}/static/plugin/layer/laydate/laydate.js"></script>
    <script type="text/javascript">
    var testplanId = parent.listTree_nodeId;
    var layerFlag = "";
    console.log("testplanId: "+testplanId);
  //给弹框添加回车提交事件
    $(function(){ 
    	$(document).keydown(function(event){ 
	    	if(event.keyCode==13){
	    		window.event.returnValue = false;
	    		if(layerFlag == "cloneTestPlan"){
	    			saveCloneTestPlan();
	    		}else if(layerFlag == ""){
	    			submit();
	    		}
	    		
	    	} 
    	}); 
});

    $(function(){
    	/*表单验证  */
    	//表单验证
    		 $('#openAndEditTestPlanForm').bootstrapValidator({
    	   		   message: 'This value is not valid',
    	           feedbackIcons: {
    	        	   valid:'glyphicon glyphicon-ok',
    	        	   invalid:'glyphicon glyphicon-remove',
    	        	   validating:'glyphicon glyphicon-refresh'
    	           },
    	           excluded: [':disabled'],
    	            fields: {
    	            	testPlanName :{
    						message : '计划名称验证失败',
    						validators : {
    							notEmpty : {//非空验证：提示消息
    								message : '计划名称不能为空'
    							},
    							stringLength: {/*长度提示*/
    							    min: 1,
    							    max: 100,
    							    message: '计划名称长度必须在1到100个字之间'
    							   }
    						}
    					},
    					summary :{
    						message : '摘要验证失败',
    						validators : {
    							stringLength: {/*长度提示*/
    							    min: 1,
    							    max: 255,
    							    message: '摘要长度必须在1到255个字之间'
    							   }
    						}
    					}

    	               }
    	        });
    	
    		 $('#cloneTestPlanForm').bootstrapValidator({
  	   		   message: 'This value is not valid',
  	           feedbackIcons: {
  	        	   valid:'glyphicon glyphicon-ok',
  	        	   invalid:'glyphicon glyphicon-remove',
  	        	   validating:'glyphicon glyphicon-refresh'
  	           },
  	           excluded: [':disabled'],
  	            fields: {
  	            	projectId :{
  						message : '项目验证失败',
  						validators : {
  							notEmpty : {//非空验证：提示消息
  								message : '项目不能为空'
  							}
  						}
  					},
  					planName :{
  						message : '计划名称验证失败',
  						validators : {
  							notEmpty : {//非空验证：提示消息
  								message : '计划名称不能为空'
  							},
  							stringLength: {/*长度提示*/
  							    min: 1,
  							    max: 100,
  							    message: '计划名称长度必须在1到100个字之间'
  							   }
  						}
  					}

  	               }
  	        });
    	})

 

//初始化加载页面
	$(function(){
		getAuthUser();
		getTestPlan();
		getProjectByUser();
  });

  //根据项目获取用户列表
  function getAuthUser(){
  	$.ajax({
  	    type : "get",
  	    async: false,
  	    url : path+'/basic/getAuthUser4Project',//找到controller里相应的方法
  	    beforeSend: function(XMLHttpRequest){

      	},
  	    success : function(dataList) {
  	    	//加载报告人下拉列表
		    	$("#assignId").html("");
		    	$(dataList).each(function(index,data){
		    		$("#assignId").append("<option value="+data.userId+">"+data.userName+"</option>");
				});

  	    },
  	    complete: function(XMLHttpRequest, textStatus){


      	}
  	   });
  };

  //测试计划编辑初始信息获取
  function getTestPlan(){
// 	  console.log("testplanId: "+testplanId);
	  	$.ajax({
	  	    type : "get",
	  	    async: false,
	  	    url : path+'/testCaseController/getTestPlan?testplanId='+testplanId,//找到controller里相应的方法
	  	    beforeSend: function(XMLHttpRequest){

	      	},
	  	    success : function(dataMap) {
	  	    	console.log("dataMap: "+JSON.stringify(dataMap));
	  	    	for(var key in dataMap){
		    		if(key == "testplanId"){
		    			$('#testplanId').val(dataMap[key]);
		    			console.log($('#testplanId').val());
		    		}else if(key == "name"){
		    			$('#testPlanName').val(dataMap[key]);
	    			console.log($('#testPlanName').val());
		    		}else if(key == "assignId"){
		    			$('#assignId').val(dataMap[key]);
		    			console.log($('#assignId').val());
		    		}else if(key == "state"){
		    			$('#state').val(dataMap[key]);
		    			console.log($('#state').val());
		    		}else if(key == "summary"){
		    			$('#summary').val(dataMap[key]);
		    			console.log($('#summary').val());
		    		}else if(key == "beginTime"){
		    			var beginTime = formatDate(parseInt(dataMap[key]));
		    			$('#beginTime').val(beginTime);
		    			console.log($('#beginTime').val());
		    		}else if(key == "endTime"){
		    			var endTime = formatDate(parseInt(dataMap[key]));
		    			$('#endTime').val(endTime);
		    			console.log($('#endTime').val());
		    		}
		    	}

	  	    },
	  	    complete: function(XMLHttpRequest, textStatus){


	      	}
	  	   });
	  };

  //测试计划编辑页面的克隆按钮触发
    function cloneTestPlan(){
    	$('#projectId').val("");
		$('#planName').val("");
    	$('#cloneTestPlanForm').data('bootstrapValidator').resetForm(true);
    	layer.open({
    		type : 1,
    		title :'克隆测试计划',
    		fix : true,
    		scrollbar : false,
//     		maxmin: true, //开启最大化最小化按钮
    		area : [ '500px', '280px' ],
    		shift : 2,
    		content : $('#cloneTestPlanHtml'),
    		btn:['确定','取消'],
    		success: function(layero, index){
    			if(!$('.layui-layer-title').has('span').length){
    				 $(".layui-layer-title").prepend("<span class='glyphicon glyphicon-th-large' ></span>&nbsp;");
    			 }
    			layerFlag = "cloneTestPlan";
    	  	},
    		end : function() {
    			layerFlag = "";
    		},
    		yes: function(index, layero){
    			saveCloneTestPlan();
    			
    		  },
    		btn2: function(index, layero){
    		    //按钮【按钮二】的回调
    		  },
    	});
    	};
    	//根据权限获取项目列表
    	function getProjectByUser(){
    		$.ajax({
			    type : "get",
			    async: false,
			    url : path+'/basic/getProjectByUser',//找到controller里相应的方法
			    beforeSend: function(XMLHttpRequest){

	        	},
			    success : function(dataList) {
			    	console.log("dataList: "+JSON.stringify(dataList));
    		    	//加载计划下拉列表
    		    	$("#projectId").html("<option value=''>请选择</option>");
    		    	$(dataList).each(function(index,data){
    		    		$("#projectId").append("<option value="+data.projectId+">"+data.projectName+"</option>");
    				});
			    },
			    complete: function(XMLHttpRequest, textStatus){


	        	}
			   });

    	};
    	//克隆弹框保存事件
    	function saveCloneTestPlan(){

    		
    		var bootstrapValidator = $("#cloneTestPlanForm").data('bootstrapValidator').validate();
	    	if(bootstrapValidator.isValid()){
	    		var params = {
	    				"testplanId" : testplanId,
	    				"cloneProjectId" : $('#projectId').val(),
	    				"clonePlanName" : $('#planName').val()
	    		};
	    		console.log("params: "+JSON.stringify(params));
	    		$.ajax({
	    	        type : "post",
	    	        url :path+'/testCaseController/cloneTestPlan',//找到controller里相应的方法
	    	        data : params, //把数组的值赋给data，把上面的param改为data,这里就能直接去掉
	    	        dataType : "json",//根据数据类型选择text、jason等
	    	        success : function(data) {
	    	        	console.log(data);
	    	        	if(data.result == true){
	    	        		layer.closeAll();
	    	        		if($('#projectId').val() == '${currentProjectId}'){
	    	        			parent.testPlanFreshFlag = 1;
	    	        		}
	    	        		toast.success('操作成功！','成功提示');
	    	        	}else{
	    	        		toast.error('操作失败！','失败提示');
	    	        	}

	    	        }
	    	       });
	    	}
    		
    		

    	};
    	//保存事件
    	function submit(){
    		var bootstrapValidator = $("#openAndEditTestPlanForm").data('bootstrapValidator').validate();
        	if(bootstrapValidator.isValid()){
        		var params = {
    					"testplanId":$('#testplanId').val(),
    					"name":$('#testPlanName').val(),
    					"assignId":$('#assignId').val(),
    					"state":$('#state').val(),
    					"summary":$('#summary').val(),
    					"beginTime":$('#beginTime').val(),
    					"endTime":$('#endTime').val()
    			};
    			console.log("params: "+JSON.stringify(params));
    			$.ajax({
        	        type : "post",
        	        url :path+'/testCaseController/updateTestPlan',//找到controller里相应的方法
        	        data : params, //把数组的值赋给data，把上面的param改为data,这里就能直接去掉
        	        dataType : "json",//根据数据类型选择text、jason等
        	        success : function(data) {
        	        	console.log(data);
        	        	if(data.result == true){
        	        		parent.testPlanFreshFlag = 1;
        	        		parent.testPlan_left.testplanIdChange();
    		        		parent.layer.closeAll();
    		        		parent.toast.success('操作成功！','成功提示');
    		        	}else{
    		        		toast.error('操作失败！','失败提示');
    		        	}
        	        }
        	       });
        	}
			


    	}


    	//时间戳转换
    	function add0(m){return m<10?'0'+m:m }
    	function formatDate(timestamp){
    	 //timestamp是整数，否则要parseInt转换
    	      var time = new Date(timestamp);
    	      var y = time.getFullYear();
    	      var m = time.getMonth()+1;
    	      var d = time.getDate();
    	     var h = time.getHours();
    	     var mm = time.getMinutes();
    	     var s = time.getSeconds();
    	     return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
    	 }

    </script>
<jsp:include page="../common/framework/footer.jsp" />